* "Targeting Coethnic Voters, Elites or Both?"
* Unit of Analysis: Constituency-Election Year
* Dongil Lee
* 10/4/2024

* Readme:
* Effects of Aid on MP's Electoral Outcome
* Effects of Aid and MP Coethnicity on Party Switching

* Part 0: Setup

cd "/Users/dlee/Dropbox/Work/NYU/2016 Summer/MA_thesis/combined_data"

use data_with_MP_term, clear
set more off

gen min=(min_month>0)

gen min_first_yr=(min_month_first_yr>0)

rename win ruling

* Declare control variables
global c1 logpop infant /* Time-variant constituency-level variables */
global c2 gender ruling same_const min /* Time-variant MP-level variables */

* Replace some values in gov_to_opp and opp_to_gov
replace gov_to_opp = 0 if gov_to_opp == 9 /* "Unidentified" assumed to be not switched */
replace opp_to_gov = 0 if opp_to_gov == 9 /* "Unidentified" assumed to be not switched */

* First-time MP
gen first_timer=0
replace first_timer=1 if same_const==0

* Never elected before
gen first_elec = 0
replace first_elec = 1 if n_term == 1

* Region factor variable
encode region, gen(region_num)

* Margin by election term
sort const_code year
by const_code: gen lag_turnout = turnout[_n-1]
by const_code: gen lead_turnout = turnout[_n+1]

* Margin by election term
sort const_code year
by const_code: gen lag_margin = margin[_n-1]
by const_code: gen lead_margin = margin[_n+1]

* Government share by election term
sort const_code year
by const_code: gen lag_govshare = govshare[_n-1]
by const_code: gen lead_govshare = govshare[_n+1]

* Opposition share by election term
sort const_code year
by const_code: gen lag_oppshare = oppshare[_n-1]
by const_code: gen lead_oppshare = oppshare[_n+1]

* Opposition share by election term
sort const_code year
by const_code: gen lag_same_const = same_const[_n-1]
by const_code: gen lead_same_const = same_const[_n+1]

* Ctrl trend
xtset const_code year

* Declare control variables
global c1 logpop infant /* Time-variant constituency-level variables */
global c2 gender same_const min ruling /* Time-varaint (by regime) MP-level variables */
global c2_first_yr gender same_const min_first_yr ruling /* Time-varaint (by regime) MP-level variables */

global rgm regime1 regime2 regime3
global rgn southern northern central

* Combine all control variables
global ctrl $c1 $c2 i.year

* Decare Independent variables
global aid log_d_aidpc

* Label the variables
la var log_c_aidpc "Committed Aid pc (log)"
la var log_d_aidpc "Disbursed Aid pc (log)"
la var margin "Victory Margin"
la var govshare "Incumbent Vote Share"
la var oppshare "Opposition Vote Share"
la var turnout "Voter Turnout"
la var coethnic "Coethnic Voter (Dummy)"
la var coethnic_c "Coethnic Voter (Share)"
la var elf "Ethnic Diversity"
la var seg "Ethnic Segregation"
la var logpop "Population Density (log)"
la var infant "Infant Mortality Rate (log)"
la var cropland "Cropland Area pc (log)"
la var elevation "Elevation (km)"
la var distance "Distance (km)"
la var logarea "Land Area (log square km)"
la var rural "Proportion of Rural Area (%)"
la var hometown "Hometown of President"
la var northern "Northern Region"
la var central "Central Region"
la var southern "Southern Region"
la var regime1 "Muluzi regime (99-03)"
la var regime2 "Mutharika I regime (04-08)"
la var regime3 "Mutharika II regime (09-10)"
la var log_gdppc "GDP pc (log)"
la var log_net_odapc "ODA pc (log)"
la var log_net_bipc "Bilateral Aid pc (log)"
la var gender "Female MP"
la var n_term "Number of Terms MP served"
la var seniority "MP Seniority"
la var MPcoethnic "Coethnic MP"
la var ruling "Copartisan MP"
la var switched_ever "MP switched party?"
la var same_const "MP Seniority"
la var min "MP Minister"
la var min_first_yr "MP Minister (1st yr)"
la var first_timer "First-time MP"
la var lead_govshare "Incumbent Vote Share (t+1)"
la var lead_oppshare "Opposition Vote Share (t+1)"
la var lead_margin "Victory Margin (t+1)"

* Set graph style for data visualization
set scheme burd5

cd "/Users/dlee/Dropbox/Work/NYU/2016 Summer/MA_thesis/output"

* Table A7. Summary Statistics

* Define a set of characteristics variables for difference estimation
global sumstat $aid $aid2 MPcoethnic coethnic_c coethnic logpop infant gender same_const min ruling first_timer govshare oppshare margin lead_govshare lead_oppshare lead_margin

eststo clear
eststo r0: quietly estpost summarize $sumstat

estout r0 using summary_election_year.xls, replace ///
	cells("mean(pattern(1 1 1 1) fmt(2)) sd(pattern(1 0 0 0)) min(pattern(1 0 0 0)) max(pattern(1 0 0 0))") label
	
* Table A8. Summary Stat by First-timer and Incumbent MPs

* Define a set of characteristics variables for difference estimation
global sumstat $aid $aid2 MPcoethnic coethnic_c coethnic logpop infant gender min ruling

eststo clear

* Summary statistics for first_timer == 0
eststo firsttimer0: quietly estpost summarize $sumstat if first_timer == 0

* Summary statistics for first_timer == 1
eststo firsttimer1: quietly estpost summarize $sumstat if first_timer == 1

* Perform t-tests for the specified variables and store the results
eststo diff: quietly estpost ttest $sumstat, by(first_timer) unequal

* Export the results to an Excel (.xls) file with standard errors, without repeated headers
estout firsttimer0 firsttimer1 diff using "summary_MP_incumbency.xls", replace ///
	cells("mean(pattern(1 0 0 0) fmt(2)) mean(pattern(0 1 0 0) fmt(2))" ///
    "b(star pattern(0 0 1 0) fmt(2)) se(pattern(0 0 1 0) fmt(2))") ///
    label nonumbers

* Table 5.	
	
* Part I: Effects of MP Coethnicity on MP's Party Switching 
* (from Opposition to Government Party)

set more off
estimates drop _all

eststo est1: logit opp_to_gov MPcoethnic i.region_num if year==2004, vce(robust)
eststo est2: logit opp_to_gov MPcoethnic i.region_num gender same_const min_first_yr if year==2004, vce(robust)
eststo est3: logit opp_to_gov ruling i.region_num if year==2004, vce(robust)
eststo est4: logit opp_to_gov ruling i.region_num $c2_first_yr if year==2004, vce(robust)
eststo est5: logit opp_to_gov MPcoethnic ruling i.region_num $c2_first_yr if year==2004, vce(robust)

estadd local region_fe "\checkmark" : est*

*cd "C:\Users\dl\Dropbox\2016 Summer\MA_thesis\output"
cd "/Users/dlee/Dropbox/Work/NYU/2016 Summer/MA_thesis/output"

estout est* ///
	using pswitch_main.xls, replace ///
	keep(MPcoethnic ruling gender same_const min_first_yr) ///
	order(MPcoethnic ruling gender same_const min_first_yr) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) ///
	label cells(b(star fmt(2)) se(par(`"="("'`")""') fmt(%9.2f))) ///
	stats(region_fe N, ///
	labels("Region FE" "N") ///
	fmt(%9.0g %9.0g %9.0g))
	

* Figure A9.
* Graph

logit opp_to_gov MPcoethnic i.region_num $cc2_first_yr if year==2004, vce(robust)

margins, at(MPcoethnic=(0 1)) atmeans 
marginsplot, ///
title("") ///
ytitle("Prob(Joining DPP)") ///
xtitle("MP Coethnicity (1: Match, 0: No-match)") ///
yline(0)

graph export "pswitch_main_graph.pdf", replace as(pdf)

* Robustness check I: Excluding uncertain cases
* (from Opposition to Government Party)

* Table A19.

set more off
estimates drop _all

eststo est1: logit opp_to_gov MPcoethnic i.region_num if year==2004 & switched_ever != 9, vce(robust)
eststo est2: logit opp_to_gov MPcoethnic i.region_num gender same_const min_first_yr if year==2004 & switched_ever != 9, vce(robust)
eststo est3: logit opp_to_gov ruling i.region_num if year==2004 & switched_ever != 9, vce(robust)
eststo est4: logit opp_to_gov ruling i.region_num $c2_first_yr if year==2004 & switched_ever != 9, vce(robust)
eststo est5: logit opp_to_gov MPcoethnic ruling i.region_num $c2_first_yr if year==2004 & switched_ever != 9, vce(robust)

estadd local region_fe "\checkmark" : est*

*cd "C:\Users\dl\Dropbox\2016 Summer\MA_thesis\output"
cd "/Users/dlee/Dropbox/Work/NYU/2016 Summer/MA_thesis/output"

estout est* ///
	using pswitch_main_rc.xls, replace ///
	keep(MPcoethnic ruling gender same_const min_first_yr) ///
	order(MPcoethnic ruling gender same_const min_first_yr) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) ///
	label cells(b(star fmt(2)) se(par(`"="("'`")""') fmt(%9.2f))) ///
	stats(region_fe N, ///
	labels("Region FE" "N") ///
	fmt(%9.0g %9.0g %9.0g))


* Part II: Effects of Coethnic MP on Electoral Outcomes

* Table 4.

set more off
estimates drop _all

eststo est1: xtreg lead_govshare MPcoethnic $ctrl govshare, fe vce(cluster const_code)
eststo est2: xtreg lead_govshare MPcoethnic coethnic_c $ctrl govshare, fe vce(cluster const_code)

eststo est3: xtreg lead_oppshare MPcoethnic $ctrl oppshare, fe vce(cluster const_code)
eststo est4: xtreg lead_oppshare MPcoethnic coethnic_c $ctrl oppshare, fe vce(cluster const_code)

eststo est5: xtreg lead_margin MPcoethnic $ctrl margin, fe vce(cluster const_code)
eststo est6: xtreg lead_margin MPcoethnic coethnic_c $ctrl margin, fe vce(cluster const_code)

estadd local const_fe "\checkmark" : est*
estadd local year_fe "\checkmark" : est*

estadd local const_ctrl "\checkmark" : est*

*cd "C:\Users\dl\Dropbox\2016 Summer\MA_thesis\output"
cd "/Users/dlee/Dropbox/Work/NYU/2016 Summer/MA_thesis/output"

estout est* ///
	using MPcoethnic_election_main.xls, replace ///
	keep(MPcoethnic coethnic_c ruling gender same_const min govshare oppshare margin) ///
	order(MPcoethnic coethnic_c ruling gender same_const min govshare oppshare margin) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) ///
	label cells(b(star fmt(2)) se(par(`"="("'`")""') fmt(%9.2f))) ///
	stats(const_fe year_fe const_ctrl r2 N, ///
	labels("Constituency FE" "Year FE" "Const. Control" "\$R^2\$" "N") ///
	fmt(%9.0g %9.0g %9.0g %9.2f %9.0g))
	
* Part III: Effects of Aid on Electoral Outcomes

* Table A20.

set more off
estimates drop _all

eststo est1: xtreg lead_govshare $aid $ctrl govshare, fe vce(cluster const_code)
eststo est2: xtreg lead_govshare $aid coethnic_c $ctrl govshare, fe vce(cluster const_code)

eststo est3: xtreg lead_oppshare $aid $ctrl oppshare, fe vce(cluster const_code)
eststo est4: xtreg lead_oppshare $aid coethnic_c $ctrl oppshare, fe vce(cluster const_code)

eststo est5: xtreg lead_margin $aid $ctrl margin, fe vce(cluster const_code)
eststo est6: xtreg lead_margin $aid coethnic_c $ctrl margin, fe vce(cluster const_code)

estadd local const_fe "\checkmark" : est*
estadd local year_fe "\checkmark" : est*

estadd local const_ctrl "\checkmark" : est*

*cd "C:\Users\dl\Dropbox\2016 Summer\MA_thesis\output"
cd "/Users/dlee/Dropbox/Work/NYU/2016 Summer/MA_thesis/output"

estout est* ///
	using aid_election_main.xls, replace ///
	keep($aid coethnic_c ruling gender same_const min govshare oppshare margin) ///
	order($aid coethnic_c ruling gender same_const min govshare oppshare margin) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) ///
	label cells(b(star fmt(2)) se(par(`"="("'`")""') fmt(%9.2f))) ///
	stats(const_fe year_fe const_ctrl r2 N, ///
	labels("Constituency FE" "Year FE" "Const. Control" "\$R^2\$" "N") ///
	fmt(%9.0g %9.0g %9.0g %9.2f %9.0g))

* Table 6.	
	
* Conditional Effects of Aid on Electoral Outcomes by MP Seniority

set more off
estimates drop _all

eststo est1: xtreg lead_govshare c.$aid##i.first_timer  $ctrl govshare, fe vce(cluster const_code)
eststo est2: xtreg lead_govshare c.$aid##i.first_timer coethnic_c $ctrl govshare, fe vce(cluster const_code)

eststo est3: xtreg lead_oppshare c.$aid##i.first_timer  $ctrl oppshare, fe vce(cluster const_code)
eststo est4: xtreg lead_oppshare c.$aid##i.first_timer coethnic_c $ctrl oppshare, fe vce(cluster const_code)

eststo est5: xtreg lead_margin c.$aid##i.first_timer  $ctrl margin, fe vce(cluster const_code)
eststo est6: xtreg lead_margin c.$aid##i.first_timer coethnic_c $ctrl margin, fe vce(cluster const_code)

estadd local const_fe "\checkmark" : est*
estadd local year_fe "\checkmark" : est*

estadd local const_ctrl "\checkmark" : est*

*cd "C:\Users\dl\Dropbox\2016 Summer\MA_thesis\output"
cd "/Users/dlee/Dropbox/Work/NYU/2016 Summer/MA_thesis/output"

estout est* ///
	using aid_election_first_timer_main.xls, replace ///
	keep(1.first_timer#c.$aid $aid 1.first_timer coethnic_c ruling gender same_const min govshare oppshare margin) ///
	order(1.first_timer#c.$aid $aid 1.first_timer coethnic_c ruling gender same_const min govshare oppshare margin) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) ///
	label cells(b(star fmt(2)) se(par(`"="("'`")""') fmt(%9.2f))) ///
	stats(const_fe year_fe const_ctrl r2 N, ///
	labels("Constituency FE" "Year FE" "Const. Control" "\$R^2\$" "N") ///
	fmt(%9.0g %9.0g %9.0g %9.2f %9.0g))

